package com.harboursoftware.xstorage.unit.db.sql

import junit.framework.Assert;

import com.harboursoftware.xstorage.TestDatabaseProvider;
import com.harboursoftware.xstorage.db.sql.UserSqlDao 
import com.harboursoftware.xstorage.model.XUser 
import org.junit.After 
import org.junit.Before 
import org.junit.Test 

class UserSqlDaoTest {
    UserSqlDao userDao
    def databaseProvider = new TestDatabaseProvider()
    
    def user
    @Before
    void setUp() {
        userDao = new UserSqlDao(databaseProvider)
        databaseProvider.recreateDatabase()
        
        //test user
        user = new XUser()
        user.accessKeyId = '0PN5J17HBGZHT7JJ3X83'
        user.secretAccessKey = 'uV3F3YluFJax1cknvbcGwgjvx4QpvB+leU8dUj2o'
        user.email = 'test@harboursoftware.com'
        user.displayName = "display@harboursoftware.com"
    }
    
    @After
    void tearDown() {
        userDao = null
        user = null
    }
        
    @Test
    void test_saveUser() {
        userDao.saveUser user
        
        def persistentedUser = userDao.findUserByAccessKeyId(user.accessKeyId)
        Assert.assertEquals(user.accessKeyId, persistentedUser.accessKeyId)
        Assert.assertEquals(user.secretAccessKey, persistentedUser.secretAccessKey)
        Assert.assertEquals(user.email, persistentedUser.email)
        Assert.assertEquals(user.displayName, persistentedUser.displayName)
    }
    
    @Test
    void test_findUserByEmail() {
        userDao.saveUser user
        
        def persistentedUser = userDao.findUserByEmail('test@harboursoftware.com')
        Assert.assertEquals(user.accessKeyId, persistentedUser.accessKeyId)
        Assert.assertEquals(user.secretAccessKey, persistentedUser.secretAccessKey)
        Assert.assertEquals(user.email, persistentedUser.email)
        Assert.assertEquals(user.displayName, persistentedUser.displayName)
    }

}
